package com.sixteen.ordering_system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sixteen.ordering_system.mapper.*;
import com.sixteen.ordering_system.pojo.*;
import com.sixteen.ordering_system.service.ISysUserService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*
 *
 * 视图跳转
 * */
@Controller
public class ViewController {



    @RequestMapping({"/login","/bus/login"})
    public String login(){
        return "login";
    }

    @Resource
    private DishMapper dishMapper;
   @Resource
   private DishCategoryMapper dishCategory;
   @Resource
   private SysUserMapper sysUserMapper;
    @RequestMapping("/index")
    public ModelAndView index(@RequestParam(defaultValue = "0")Integer dishCategoryId) {
        ModelAndView modelAndView = new ModelAndView();
//        List<Dish> dishes = dishMapper.selectList(null);
        List<Dish> foundDish = null;
        if (dishCategoryId == 0) {
            foundDish = dishMapper.selectCategoryall();
        }else {
           foundDish =  dishMapper.selectCategory(dishCategoryId);
        }
        List<DishCategory> selectCategory=dishCategory.selectCategory();
//        List<SysUser> selectByRemark=sysUserMapper.selectByRemark(sysUserId,remark);
        modelAndView.addObject("category",selectCategory);
        modelAndView.addObject("id",dishCategoryId);
        modelAndView.addObject("dishList",foundDish);
//        modelAndView.addObject("remark",selectByRemark);
        modelAndView.setViewName("index");
//        modelAndView.addObject("dishList", dishes);
        return modelAndView;
    }

    @RequestMapping("menu")
    public ModelAndView menu(@RequestParam(defaultValue = "0") Integer dishCategoryId) {
        ModelAndView modelAndView = new ModelAndView();
        List<Dish> Dish = null;
        if (dishCategoryId == 0) {
          Dish = dishMapper.selectCategoryall();
        }else {
            Dish =  dishMapper.selectCategory(dishCategoryId);
        }
        List<DishCategory> selectCategory=dishCategory.selectCategory();
        modelAndView.addObject("category",selectCategory);
        modelAndView.addObject("id",dishCategoryId);
        modelAndView.addObject("dishList",Dish);
        modelAndView.addObject("dishList",Dish);
        modelAndView.setViewName("menu");
        return modelAndView;
    }

    @Autowired
    DishCartMapper dishCartMapper;
    @RequestMapping("shopping")
    public ModelAndView shopping(HttpSession session, Dish dish,Integer dishId){
        ModelAndView modelAndView = new ModelAndView();
        SysUser sysUser = (SysUser) session.getAttribute("loginUser");
        List<DishCart> Cart = null;
        if (sysUser.getSysUserId()>0) {
            Cart = dishCartMapper.cartAll(sysUser.getSysUserId());
//            System.out.println(Cart);
        }else {
            Cart = null;
        }
        modelAndView.addObject("cartList",Cart);
        modelAndView.setViewName("shopping");
        return modelAndView;

    }

    @Autowired
    private SysOrderMapper sysOrderMapper;
    @RequestMapping("ordering")
    public ModelAndView ordering(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        SysUser User = (SysUser) session.getAttribute("loginUser");
        List<SysOrder> Order =sysOrderMapper.orderAll(User.getSysUserId());
        modelAndView.addObject("orderList",Order);
        modelAndView.setViewName("ordering");
        return modelAndView;
    }




    @RequestMapping("/erropage")
    public String erropage() {
        return "erropage.html";
    }

    @RequestMapping("/administrators")
    public String administrators() {
        return "administrators.html";
    }

    @RequestMapping("/business_login")
    public String business_login() {
        return "business_login";
    }

    @RequestMapping("/order_details")
    public String order_details() {
        return "order_details";
    }
}

